package com.ccgk.logistics.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ccgk.common.page.PaginationVo;
import com.ccgk.logistics.domain.Order;
import com.ccgk.logistics.domain.OrderExample;
import com.ccgk.logistics.persistence.OrderMapper;
import com.ccgk.logistics.service.OrderService;
@Service
public class OrderServiceImpl implements OrderService{
	
	@Autowired
	private OrderMapper orderMapper;
	
	@Override
	public Integer add(Order order) {
		return orderMapper.insert(order);
	}

	@Override
	public void batchDelete(Integer[] idIntArray) {
		for(Integer id:idIntArray){
			delete(id);
		}
		
	}

	@Override
	public Integer delete(int id) {
		return orderMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PaginationVo<Order> findList(OrderCriteria criteria,
			int currentPage, int pageSize) {
		OrderExample example = new OrderExample();
		OrderExample.Criteria _criteria = example.createCriteria();
		Map<Integer,Object> params = criteria.getParams();
		
		for(Integer key : params.keySet()){
			if(key.equals(1) && StringUtils.isNotEmpty(params.get(key).toString())){
				_criteria.andOrderCodeLike(params.get(key).toString());
			}
			if(key.equals(2) && StringUtils.isNotEmpty(params.get(key).toString())){
				_criteria.andMblCodeLike(params.get(key).toString());
			}
			if(key.equals(3)){
				_criteria.andCustomerIdEqualTo((Integer)params.get(key));
			}
		}
		
		List<Order> data = orderMapper.selectByExample(example,new RowBounds((currentPage-1)*pageSize, pageSize));
		int recordCount = orderMapper.countByExample(example);
		PaginationVo<Order> result = new PaginationVo<Order>(data, recordCount, pageSize, currentPage);
		return result;
	}

	@Override
	public Order getById(int id) {
		return orderMapper.selectByPrimaryKey(id);
	}

	@Override
	public Integer update(Order order) {
		return orderMapper.updateByPrimaryKey(order);
		//return orderMapper.updateByPrimaryKeySelective(order);
	}

	@Override
	public Order getByOrderCode(String orderCode) {
		OrderExample example = new OrderExample();
		example.createCriteria().andOrderCodeLike(orderCode);
		List<Order> result = orderMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(result))
			return null;
		else
			return result.get(0);
	}

}
